package com.bss.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.andy.basics.Utils;
import com.bss.dao.PatientDao;
import com.bss.model.Patient;
import com.bss.service.PatientService;

@Controller
@Scope("session")
@RequestMapping("/images")
public class ImageController {

	private static final String IMAGE_TYPE = "jpg";

	private static final int IMAGE_HEIGHT = 240;

	private static final int IMAGE_WIDTH = 320;

	private static final String COLUMN_SEPARATOR = ";";

	private static final String ROW_SEPARATOR = "\\|";

	public static final String IMAGE_FILE = "imageFile";

	@Autowired
	private Utils utils;

	@Autowired
	private PatientDao patientDao;

	@Autowired
	private PatientService patientService;

	public ImageController() {
	}

	public Utils getUtils() {
		return this.utils;
	}

	public void setUtils(final Utils utils) {
		this.utils = utils;
	}

	@RequestMapping("/click2")
	@ResponseBody
	public String click2(final InputStream iis) throws Exception {

		ByteArrayOutputStream bos = new ByteArrayOutputStream(1024 * 8);

		byte data[] = new byte[1024];
		while (true) {

			int n = iis.read(data);
			if (n <= 0) {
				break;
			}
			bos.write(data, 0, n);
		}

		this.patientService.setImageData(bos.toByteArray());

		return "bss/images/clicked2?id=" + new Date().getTime();
	}

	@RequestMapping("/clicked2")
	public ResponseEntity<byte[]> clicked2() throws Exception {
		try {
			HttpHeaders responseHeaders = new HttpHeaders();
			responseHeaders.setContentType(MediaType.IMAGE_JPEG);
			responseHeaders.setContentLength(this.patientService.getImageData().length);

			return new ResponseEntity<byte[]>(this.patientService.getImageData(), responseHeaders, HttpStatus.CREATED);
		} catch (Exception e) {
			return new ResponseEntity<byte[]>(new byte[0], null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("/click")
	@ResponseBody
	public String click(@RequestParam("data1") final String data) throws Exception {

		BufferedImage image = this.utils.createImage(data, ImageController.IMAGE_WIDTH, ImageController.IMAGE_HEIGHT,
				ImageController.ROW_SEPARATOR, ImageController.COLUMN_SEPARATOR);

		File file = this.utils.saveImageInTempFile(image, ImageController.IMAGE_TYPE);

		if ((this.patientService.getImageFile() != null) && this.patientService.getImageFile().exists()) {
			this.patientService.getImageFile().delete();
		}

		this.patientService.setImageFile(file);

		return "bss/images/clicked?imageName=" + file.getName();
	}

	@RequestMapping("/clicked")
	public ResponseEntity<byte[]> clicked() throws Exception {

		File file = this.patientService.getImageFile();

		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.setContentType(MediaType.IMAGE_JPEG);
		responseHeaders.setContentLength((int) file.length());

		byte imageData[] = this.utils.readBytes(file);

		this.patientService.setImageData(imageData);

		return new ResponseEntity<byte[]>(imageData, responseHeaders, HttpStatus.CREATED);
	}

	@RequestMapping("/saved")
	public ResponseEntity<byte[]> saved(@RequestParam("id") final String id, final ServletRequest request) throws Exception {

		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.setContentType(MediaType.IMAGE_JPEG);

		byte[] imageData = null;

		try {
			Patient patient = this.patientDao.findById(Integer.parseInt(id));
			imageData = patient.getPhoto();
			if (imageData == null) {
				imageData = this.getNoPhotoData(request);
			}
		} catch (Exception e) {
			imageData = this.getNoPhotoData(request);
		}

		responseHeaders.setContentLength(imageData == null ? 0 : imageData.length);

		return new ResponseEntity<byte[]>(imageData, responseHeaders, HttpStatus.CREATED);
	}

	protected ApplicationContext getContext(final ServletRequest request) {
		return RequestContextUtils.getWebApplicationContext(request);
	}

	protected byte[] getNoPhotoData(final ServletRequest request) throws IOException {
		return this.utils.readBytes(this.getContext(request).getResource("/images/no-photo.gif").getFile());
	}

	public void setPatientDao(final PatientDao patientDao) {
		this.patientDao = patientDao;
	}

	public PatientDao getPatientDao() {
		return this.patientDao;
	}

	public void setPatientService(final PatientService patientService) {
		this.patientService = patientService;
	}

	public PatientService getPatientService() {
		return this.patientService;
	}

	@RequestMapping("/tr")
	@ResponseBody
	public String tr() {
		return "Helloo";
	}
}
